package com.ucampus.dao;

import com.ucampus.entity.user;
import com.ucampus.entity.userinfo;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;

@Mapper
public interface userLoginMapper {
    // 根据手机号查询id
    @Select("SELECT id FROM user WHERE userPhone = #{userPhone}")
    Integer getIdByPhone(@Param("userPhone") String phone);

    // 根据微信号查询id
    @Select("SELECT id FROM user WHERE userWX = #{userWX}")
    Integer getIdByWX(@Param("userWX") String wx);

    // 根据QQ号查询id
    @Select("SELECT id FROM user WHERE userQQ = #{userQQ}")
    Integer getIdByQQ(@Param("userQQ") String qq);

    // 验证 id 和 password
    @Select("SELECT COUNT(*) FROM user WHERE id = #{id} AND userpassword = #{userpassword}")
    Integer validateLogin(@Param("id") long id, @Param("userpassword") String userpassword);

    // 注册新用户的信息
    @Insert("INSERT INTO user (id, userpassword, userPhone, createtime, token, expiretime) " +
            "VALUES (#{id}, #{userpassword}, #{userPhone}, #{createtime}, #{token}, #{expiretime})")
    void insertUser(user user);

    // 更新用户信息
    @Update("UPDATE user SET userPhone = #{userPhone}, userQQ = #{userQQ}, userWX = #{userWX} WHERE id = #{id}")
    void updateUser(user user);

    // 根据用户id查询所有内容
    @Select("SELECT * FROM user WHERE id = #{id}")
    user findUserById(@Param("id") long id);

    // 根据用户 token 查询用户信息
    @Select("SELECT * FROM user WHERE token = #{token}")
    user findUserByToken(@Param("token") String token);

    @Select("SELECT * FROM userinfo WHERE id = #{id}")
    userinfo findUserInfoById(@Param("id") long id);

    // 更新用户令牌信息，用于令牌刷新或重置操作
    @Update("UPDATE user SET token = #{token}, createtime = #{createtime},expiretime = #{expiretime} WHERE id = #{id}")
    void updateToken(@Param("id") long id, @Param("token") String token, @Param("createtime") LocalDateTime createtime,@Param("expiretime") LocalDateTime expiretime);

    // 根据用户ID获取加密后的密码
    @Select("SELECT userpassword FROM user WHERE id = #{id}")
    String getEncryptedPasswordById(long Id);
}
